//
//  UIView+Screenshot.m
//  YuiKe
//
//  Created by yuike5 yuike5 on 11-11-16.
//  Copyright (c) 2011年 yuike. All rights reserved.
//

#import "UIView+Extend.h"
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import "UIView+RectSize.h"
#import "mcf.h"

@implementation UIView (Screenshot)


- (UIImage *) imageByRenderingView {
    @try {
        UIImage *resultingImage = nil;
        //@autoreleasepool{
        CGFloat oldAlpha = self.alpha;	
        self.alpha = 1;
        
//        UIGraphicsBeginImageContext(self.bounds.size);
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();        
        self.alpha = oldAlpha;	
        return resultingImage;         
    }
    @catch (NSException *exception) 
    {
        DebugLog(@"Error: name: %@ reason: ", exception.name, exception.reason);
        return nil;
    }
    @finally 
    {
        
    }
    
}
-(UIImage*) shotImage
{ return [self imageByRenderingView]; }


- (UIImage *) imageByRenderingViewAndThreadSafe
{
//    DebugLog(@"imageByRenderingViewAndThreadSafe s");
    UIImage *returnImage = nil;    
    CGFloat oldAlpha = self.alpha;	
	self.alpha = 1;
    
    CGSize imageSize = self.bounds.size;
    void *data = malloc(imageSize.width * imageSize.height * 4);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(data, 
                                             imageSize.width, 
                                             imageSize.height, 
                                             8, /* bits per component 32/4 */
                                             imageSize.width * 4, /* bytes per row 每行字节数,每一个位图像素的代表是4个字节 */
                                             colorSpace, 
                                             kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big
                                             );
    
    
    CGColorSpaceRelease(colorSpace);
    
    // ... do your image processing here ...
    // CGContextFillRect(ctx, CGRectMake(0, 0, 10, 10));
    
    /*--------- begin --------*/    
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        //CGRect bounds = self.frame;//[window bounds];
        CGRect bounds = [self frameInAncestorView:window]; // 给出准确的坐标
        
        CALayer *layer = [window layer];
        CGContextSaveGState(ctx);
        if ([layer contentsAreFlipped]) {
            CGContextTranslateCTM(ctx, 0.0f, bounds.size.height);
            CGContextScaleCTM(ctx, 1.0f, -1.0f);
        }
        [[self layer] renderInContext:ctx];    
        CGContextRestoreGState(ctx);
    }
    /*---------- end  ------*/
    
    CGImageRef image = CGBitmapContextCreateImage(ctx);      
    returnImage = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
    
    CGContextRelease(ctx);
    free(data);
    
    self.alpha = oldAlpha;	
//    DebugLog(@"imageByRenderingViewAndThreadSafe e");
    return returnImage;
}

@end


@implementation UIView (Extend)
- (void) disableGesture
{
    self.userInteractionEnabled = FALSE;
	
	for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
		recognizer.enabled = FALSE;
	}

}

-(void) disableGestureIncludeAllSubViews
{
    [self disableGesture];
    for (UIView * subView in self.subviews) {        
        [subView disableGestureIncludeAllSubViews];
    }
}

-(void) enableGesture
{
    self.userInteractionEnabled = TRUE;
	
	for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
		recognizer.enabled = TRUE;
	}
}

-(void) enableGestureIncludeAllSubViews
{
    [self enableGesture];
    for (UIView * subView in self.subviews) {        
        [subView enableGestureIncludeAllSubViews];
    }
}

- (BOOL) rectInOtherRect: (CGRect) rect inrect:(CGRect) inRect
{    
    if (rect.origin.x < inRect.origin.x) {
        return FALSE;
    }
    
    if (rect.origin.y < inRect.origin.y) {
        return FALSE;
    }
    
    if (rect.size.width > inRect.size.width + inRect.origin.x) {
        return FALSE;
    }
    
    if (rect.size.height > inRect.size.height + inRect.origin.y) {
        return FALSE;
    }
    
    return TRUE;
}

-(void) setIntFrame:(CGRect) frame
{
    frame.origin.x = (int)(frame.origin.x + 0.5);
    frame.origin.y = (int)(frame.origin.y + 0.5);
    frame.size.width = (int)(frame.size.width + 0.5);
    frame.size.height = (int)(frame.size.height + 0.5);
    [self setFrame:frame];
}


@end